package com.example.springpethospital.entity.examination.dto;

import com.example.springpethospital.entity.examination.po.ExaminationPaperPO;
import com.example.springpethospital.entity.examination.po.ExaminationQuestionPO;
import com.example.springpethospital.entity.examination.po.ExaminationQuestionPO.Optiaon;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;

@Slf4j
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ExaminationPaperDTO {

    private Integer id;

    private String paperTitle;

    private Integer totalTime;

    private Integer totalScore;

    private List<ExaminationPaperQuestionDTO> questions;

    @EqualsAndHashCode(callSuper = true)
    @Data
    @SuperBuilder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ExaminationPaperQuestionDTO extends ExaminationQuestionPO {

        private Integer answerIndex;

        private Integer score;

        private String radio;

        private String show;

    }


    public static List<ExaminationPaperDTO> convert(Collection<ExaminationPaperPO> paperPOS, Collection<ExaminationQuestionPO> questionPOS) {
        if (CollectionUtils.isEmpty(paperPOS)) {
            return Collections.emptyList();
        }
        Map<Integer, ExaminationQuestionPO> questionPOMap = CollectionUtils.emptyIfNull(questionPOS).stream()
            .collect(Collectors.toMap(ExaminationQuestionPO::getId, Function.identity(), (a, b) -> a));
        return paperPOS.stream()
            .map(e -> ExaminationPaperDTO.builder()
                .id(e.getId())
                .paperTitle(e.getPaperTitle())
                .totalTime(e.getTotalTime())
                .totalScore(e.getTotalScore())
                .questions(ListUtils.emptyIfNull(e.getQuestions()).stream().map(a -> {
                    ExaminationQuestionPO examinationQuestionPO = questionPOMap.get(a.getQuestionId());
                    if (examinationQuestionPO == null) {
                        log.error("有试题被删除了, 此处过滤, questionId = {}", a.getQuestionId());
                        return null;
                    }
                    ExaminationPaperQuestionDTO questionDTO = new ExaminationPaperQuestionDTO();
                    BeanUtils.copyProperties(examinationQuestionPO, questionDTO);
                    Optiaon optiaon = ListUtils.emptyIfNull(examinationQuestionPO.getOptions()).stream().filter(Optiaon::getRight).findFirst().get();
                    questionDTO.setAnswerIndex(examinationQuestionPO.getOptions().indexOf(optiaon));
                    questionDTO.setScore(a.getScore());
                    questionDTO.setRadio("");
                    questionDTO.setShow("");
                    return questionDTO;
                }).filter(Objects::nonNull).collect(Collectors.toList()))
                .build()).collect(Collectors.toList());
    }

}
